package com.situ.mall.product.controller;


import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.product.Category;
import com.situ.mall.product.pojo.vo.CategoryVO;
import com.situ.mall.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author Gao
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/selectCategoryTree")
    public Result<List<CategoryVO>> selectCategoryTree() {
        List<CategoryVO> list = categoryService.selectCategoryTree();
        return Result.ok(list);
    }

    @RequestMapping("/selectByParentId/{id}")
    public Result selectByParentId(@PathVariable Long id) {
        List<CategoryVO> list =  categoryService.selectByParentId(id);
        return Result.ok(list);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.ok("更新成功");
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        categoryService.deleteById(id);
        return Result.ok("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        categoryService.add(category);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result<Category> selectById(@PathVariable Integer id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

}

